package com.tskj.mh.savefoodorder.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tskj.mh.savefoodorder.dao.mysql.DbUtilitys;
import com.tskj.mh.savefoodorder.dao.redis.RedisUtil;
import com.tskj.mh.savefoodorder.utility.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class MealsDAO {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DbUtilitys dbUtilitys;

    public JSONObject getMealInfo(String id) {
        return redisUtil.searchHashMap("meals", id);
    }

    public JSONObject getTimeInfoFromRedis(String timeId) {
        return (JSONObject)redisUtil.hget(Tools.today(), timeId);
    }

    public String getTimeSortNo(String timeId) {
        String sql = "select sortnum from reserve_time where persionid=? limit 1";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, timeId);
        if (list == null || list.size() == 0) {
            return "";
        }
        Object obj = list.get(0).get("sortnum");
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    public String getMaxOrderNo(String canteenName, String startDate, String startTime, String endTime) {
        String sql = "select (ifnull(max(sortnum), 0) + 1) sortnum from meal_order where cname=? and starttime=? and endtime=? and startdate=?";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, canteenName, startTime, endTime, startDate);
        if (list == null || list.size() == 0) {
            return "";
        }
        Object obj = list.get(0).get("sortnum");
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    /**
     * 根据食堂id从MySQL中获取食堂信息
     * @param resId
     * @return
     */
    public JSONObject getCanteenInfo(String resId) {
        String sql = "select * from (select a.*,@rank:=@rank + 1 AS canteen_rank_no " +
                "from (select * from canteen " +
                "order by canteenname) a, (select @rank:= 0) b) c where c.cid=?";
        List<Map<String, Object>> list = dbUtilitys.execSQL(sql, resId);
        if (list == null || list.size() == 0) {
            return new JSONObject();
        }
        return JSONObject.parseObject(JSONObject.toJSONString(list.get(0)));
    }

    public void saveOrder(String orderNo, String studentName, String canteenName, String mealsInfo, String mnum, String price, String startTime, String endTime, String code, String startDate, String adUserName) throws Exception {
        String id = Tools.newId();
        String sql = "insert into meal_order (orderid, sortnum, userid, cname, mname, mnum, money, starttime, endtime, qrcode, startdate, adusername, createdate, orderstate) " +
                     "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), 0)";
        dbUtilitys.execSQLWithTran(sql,
                                   id,
                                   orderNo,
                                   studentName,
                                   canteenName,
                                   mealsInfo,
                                   mnum,
                                   price,
                                   startTime,
                                   endTime,
                                   code,
                                   startDate,
                                   adUserName
                                  );
    }
}
